package com.gqz.patrol.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.patrol.app.entity.EnWzJfglEntity;
import com.gqz.patrol.app.mapper.EnWzJfglMapper;
import com.gqz.patrol.app.mapstruct.EnWzJfglMapstruct;
import com.gqz.patrol.app.service.IEnWzJfglService;
import com.gqz.patrol.vo.EnWzJfglVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 机房信息表 业务逻辑实现层ServiceImpl
 */
@Slf4j
@Service("appEnWzJfglService")
public class EnWzJfglServiceImpl extends ServiceImpl<EnWzJfglMapper, EnWzJfglEntity>
        implements IEnWzJfglService {

    @Autowired
    private EnWzJfglMapstruct mapstruct;

    @Override
    public List<EnWzJfglVo> getById(String id) {
        MPJLambdaWrapper<EnWzJfglEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnWzJfglEntity::getId, id);
        queryWrapper.selectAll(EnWzJfglEntity.class);
        List<EnWzJfglEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnWzJfglVo> page(RequestForm<EnWzJfglVo> requestForm) {
        EnWzJfglEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfglEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper
                .selectAll(EnWzJfglEntity.class);
        Page<EnWzJfglEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnWzJfglEntity> iPage = baseMapper.selectJoinPage(page, EnWzJfglEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnWzJfglVo> requestForm) {
        EnWzJfglEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfglEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnWzJfglEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnWzJfglVo> forms) {
        List<EnWzJfglEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfglEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnWzJfglVo> forms) {
        List<EnWzJfglEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfglEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    /**
     * 通过管理单位查找机房
     *
     * @param code
     * @return
     */
    @Override
    public List<EnWzJfglVo> getByYwdw(String code) {
        MPJLambdaWrapper<EnWzJfglEntity> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAll(EnWzJfglEntity.class)
                .eq(code != null, EnWzJfglEntity::getYwdwbm, code)
                .eq(EnWzJfglEntity::getIsDel, "0");
        return mapstruct.toVos(list(wrapper));
    }
}
